// 用户相关API服务
import { http } from './http';

// 接口返回类型定义
interface ApiResponse<T> {
  code: number;
  message: string;
  data: T;
}

// 分页类型
interface Pagination {
  page: number;
  pageSize: number;
  total: number;
}

// 分页数据包装类型
interface PageResult<T> {
  pagination: Pagination;
  list: T[];
}

// 用户信息类型
interface UserInfo {
  id: number;
  nickname: string;
  avatar: string;
  phone: string;
  [key: string]: any;
}

// 登录结果类型
interface LoginResult {
  token: string;
  refreshToken: string;
  userInfo: UserInfo;
}

// 订单类型
interface Trade {
  id: number;
  tradeNo: string;
  amount: number;
  status: number;
  createTime: string;
  [key: string]: any;
}

/**
 * 用户登录
 * @param params 登录参数
 */
export const login = (params: { 
  type: string;
  code: string;
  phoneCode: string;
}) => {
  return http.post<ApiResponse<LoginResult>>('/app/login', params);
};

/**
 * 获取用户信息
 */
export const getUserProfile = () => {
  return http.get<ApiResponse<UserInfo>>('/app/user/profile');
};

/**
 * 获取用户订单列表
 */
export const getUserTradeList = (params?: { 
  page?: number;
  pageSize?: number;
  status?: number;
}) => {
  return http.get<ApiResponse<PageResult<Trade>>>('/app/user/trade/list', params);
};

/**
 * 获取订单详情
 * @param tradeNo 订单号
 */
export const getUserTradeDetail = (tradeNo: string) => {
  return http.get<ApiResponse<Trade>>(`/app/user/trade/${tradeNo}`);
};

/**
 * 创建订单
 * @param params 订单参数
 */
export const createUserTrade = (params: any) => {
  return http.post<ApiResponse<Trade>>('/app/user/trade', params);
}; 